package cn.ante.pet.service.impl;

import cn.ante.basic.constant.BaseContants;
import cn.ante.basic.domain.Point;
import cn.ante.basic.exception.BusinessException;
import cn.ante.basic.utils.*;
import cn.ante.order.domain.OrderPetAcquisition;
import cn.ante.order.mapper.OrderPetAcquisitionMapper;
import cn.ante.org.domain.Shop;
import cn.ante.org.mapper.ShopMapper;
import cn.ante.pet.domain.Pet;
import cn.ante.pet.domain.SearchMasterMsg;
import cn.ante.pet.domain.SearchMasterMsgAuditLog;
import cn.ante.pet.dto.AcceptDto;
import cn.ante.pet.mapper.PetMapper;
import cn.ante.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.ante.pet.mapper.SearchMasterMsgMapper;
import cn.ante.pet.query.SearchMasterMsgQuery;
import cn.ante.pet.service.ISearchMasterMsgService;
import cn.ante.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Nai
 * @since 2022-07-19
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private PetMapper petMapper;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg) {
        // 判断信息是否为空

        // 保存寻主信息
        searchMasterMsgMapper.add(searchMasterMsg);

        // 判断信息是否合法
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(searchMasterMsg.getAddress())
                .append(searchMasterMsg.getCoatColor())
                .append(searchMasterMsg.getTitle())
                .append(searchMasterMsg.getName())
                .append(searchMasterMsg.getNote());
        if (!BaiduAuditUtils.TextCensor(stringBuffer.toString())){
            // 当审核不通过时，增加审核日志
            SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
            log.setAuditTime(new Date());
            log.setState(0);
            log.setMsgId(searchMasterMsg.getId());
            log.setNote("传入的文本信息不合规");
            searchMasterMsgAuditLogMapper.add(log);
        }else {
            // 当审核通过，将寻主任务消息状态设置为审核通过
            searchMasterMsg.setState(1);
            // 通过工具类找到距离寻主地址的经纬度
            Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
            // 找到该地址附近50公里的最近的店铺
            List<Shop> shops = shopMapper.loadAll();
            Shop shop = DistanceUtil.getNearestShop(point, shops);
            // 判断是否找到店铺,如果有店铺要将寻主任务中的shopId绑定上，不存在任务消息依旧在寻主池中，即没有shopId
            if (shop != null){
                searchMasterMsg.setShopId(shop.getId());
            }
        }
        // 不管寻主地址附近是否有店铺，最后都需要修改保存寻主任务信息
        searchMasterMsgMapper.update(searchMasterMsg);


    }

    @Override
    public void acceptOrders(AcceptDto acceptDto) {
        // 判空
        if (StringUtils.isEmpty(acceptDto.getMsgId())
                || StringUtils.isEmpty(acceptDto.getHandlerId()) || StringUtils.isEmpty(acceptDto.getNote())){
            throw new BusinessException("参数为空");
        }
        // 通过寻主消息的id判断此此消息是否存在，和此消息的状态是否还是可接单状态
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(acceptDto.getMsgId());
        if (searchMasterMsg != null && searchMasterMsg.getState() != -1){
            // 生成收购订单前，先设置好订单需要的字段值
            OrderPetAcquisition order = new OrderPetAcquisition();
            // 设置订单编号
            order.setOrderSn(CodeGenerateUtils.generateOrderSn(searchMasterMsg.getId()));
            // 设置订单的用户地址--就是寻主单用户填写的地址
            order.setAddress(searchMasterMsg.getAddress());
            // 设置订单的店铺id,就是寻主单中的店铺id
            order.setShopId(searchMasterMsg.getShopId());
//            // 设置订单的状态为待处理
//            order.setState(0);
            // 设置订单的寻主任务id就是闯进来的寻主任务id
            order.setSearchMasterMsgId(acceptDto.getMsgId());
            // 设置接单的员工id
            order.setEmpId(acceptDto.getHandlerId());
            // 设置订单处理的截至期限
            order.setLastcomfirmtime(new Date(System.currentTimeMillis() + 2*24*60*60*1000));
            // 生成用户id
            order.setUserId(searchMasterMsg.getUserId());
            // 设置订单备注摘要
            order.setDigest(acceptDto.getNote());
            // 设置订单价格
            order.setPrice(searchMasterMsg.getPrice());
            // 将信息生成宠物信息
            Pet pet = new Pet();
            // 设置宠物的名字
            pet.setName(searchMasterMsg.getName());
            pet.setCostprice(searchMasterMsg.getPrice());
            pet.setTypeId(searchMasterMsg.getPetType());
            pet.setShopId(searchMasterMsg.getShopId());
            pet.setSearchMasterMsgId(searchMasterMsg.getId());
            petMapper.add(pet);
            // 设置宠物id
            order.setPetId(pet.getId());
            orderPetAcquisitionMapper.add(order);
        }

    }

    @Override
    public void rejectOrders(Long id) {
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(id);
        if (searchMasterMsg == null){
            throw new BusinessException("此订单不存在");
        }
        searchMasterMsg.setShopId(null);
        searchMasterMsgMapper.update(searchMasterMsg);
        // 拒单之后需要添加日志记录有
    }

}
